package org.mybatis.plugins;

import org.management.utils.DateUtils;
import org.management.utils.StringUtils;
import org.mybatis.generator.api.GeneratedXmlFile;
import org.mybatis.generator.api.IntrospectedColumn;
import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.PluginAdapter;
import org.mybatis.generator.api.dom.java.Field;
import org.mybatis.generator.api.dom.java.Interface;
import org.mybatis.generator.api.dom.java.Method;
import org.mybatis.generator.api.dom.java.TopLevelClass;
import org.mybatis.generator.api.dom.xml.Document;
import org.mybatis.generator.api.dom.xml.XmlElement;

import java.util.List;

/**
 * 功能描述
 *
 * @author jy
 * @date 2019/3/1
 * @description
 * @project management
 */
public class LombokPlugin extends PluginAdapter {

    @Override
    public boolean validate(List<String> list) {
        return true;
    }

    @Override
    public boolean modelBaseRecordClassGenerated(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
        String tableComment = introspectedTable.getRemarks();
        String userName = System.getProperty("user.name");
        String date = DateUtils.getToday("yyyy/MM/dd");
        topLevelClass.addJavaDocLine(
                "/**\n" +
                        " * 功能描述\n" +
                        " * "+tableComment+"模块\n" +
                        " * @author "+userName+"\n" +
                        " * @date "+date+"\n" +
                        " * @description\n" +
                        " * @project management\n" +
                        " */");
        topLevelClass.addImportedType("lombok.*");
        topLevelClass.addImportedType("io.swagger.annotations.ApiModel");
        topLevelClass.addImportedType("javax.validation.constraints.NotEmpty");
        topLevelClass.addImportedType("io.swagger.annotations.ApiModelProperty");
        topLevelClass.addAnnotation("@Data");
        if (StringUtils.isBlank(tableComment)) {
            try {
                throw new Exception("请将表名为"+introspectedTable.getFullyQualifiedTable()+"的表注释补全!");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        topLevelClass.addAnnotation("@ApiModel(value=\""+tableComment+"\")");
        topLevelClass.addAnnotation("@EqualsAndHashCode(callSuper = true)");
        return true;
    }

    @Override
    public boolean modelFieldGenerated(Field field, TopLevelClass topLevelClass, IntrospectedColumn introspectedColumn, IntrospectedTable introspectedTable, ModelClassType modelClassType) {
        String fieldRemark = introspectedColumn.getRemarks();
        String fieldName = introspectedColumn.getJavaProperty();
        boolean required = !introspectedColumn.isNullable();
        String fieldInfo = "@ApiModelProperty(value = \""+fieldRemark+"\",name = \""+fieldName+"\"";
        if (required){
            field.addAnnotation("@NotEmpty(message = \""+fieldRemark+"不能为空\")");
            fieldInfo += ", required = true)";
        } else {
            fieldInfo += ")";
        }
        field.addAnnotation(fieldInfo);
        return super.modelFieldGenerated(field, topLevelClass, introspectedColumn, introspectedTable, modelClassType);
    }

    @Override
    public boolean clientGenerated(Interface inter, TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
        String userName = System.getProperty("user.name");
        String date = DateUtils.getToday("yyyy/MM/dd");
        String tableComment = introspectedTable.getRemarks();
        inter.addJavaDocLine(
                "/**\n" +
                        " * 功能描述\n" +
                        " * "+tableComment+"数据访问类\n" +
                        " * @author "+userName+"\n" +
                        " * @date "+date+"\n" +
                        " * @description\n" +
                        " * @project management\n" +
                        " */");
        return true;
    }

    @Override
    public boolean modelSetterMethodGenerated(Method method, TopLevelClass topLevelClass, IntrospectedColumn introspectedColumn, IntrospectedTable introspectedTable, ModelClassType modelClassType) {
        return false;
    }

    @Override
    public boolean modelGetterMethodGenerated(Method method, TopLevelClass topLevelClass, IntrospectedColumn introspectedColumn, IntrospectedTable introspectedTable, ModelClassType modelClassType) {
        return false;
    }

    @Override
    public boolean sqlMapDocumentGenerated(Document document, IntrospectedTable introspectedTable) {

        return super.sqlMapDocumentGenerated(document, introspectedTable);
    }
}
